$(document).ready(function () {
    lobstercommon.name = "lobsterlayui";
    var formid = lobstercommon.GetUrlParam("formid");
    lobsterlayui.GETAPI('tsTemplate.getformobject', { formid: formid }).then(retdata => {
        var pagejson = JSON.parse(retdata.formobj.Viewschema);
        //显示表单
        let amis = amisRequire('amis/embed');
        // 通过替换下面这个配置来生成不同页面
        let amisScoped = amis.embed('#root', pagejson, {}, {
            // 可以不传，用来实现 ajax 请求
            fetcher: (config) => {
                //var _config=config;
                return new Promise((resolve, reject) => {
                    var fetcherResult = { data: {}, status: 0, headers: {} };
                    fetcherResult.data = { status: 1, msg: '模板问卷无法提交', data: {} };
                    fetcherResult.status = res.status;
                    resolve(fetcherResult);
                })
            }
        });

        setTimeout(function () {
            html2canvas(document.body, {
                useCORS: true, // 【重要】开启跨域配置
                allowTaint: true,//允许跨域图片
                taintTest: false//是否在渲染前测试图片
            }).then(function (canvas) {
                //document.body.appendChild(canvas);
                var Pic = canvas.toDataURL("image/png");
                var imageData = Pic.replace(/^data:image\/(png|jpg);base64,/, "");

                var uploadurl='/basic/v1/oss/uploadbase64?workid=' + lobstercommon.GetWorkId() + '&appid=' + lobstercommon.GetAppId() + '&filename=headpic.png';
                $.ajax({
                    type: 'post',
                    async: false,//必须改为同步
                    url: uploadurl,
                    headers: {
                        'Content-Type': 'text/plain; charset=utf-8'
                    },
                    data: imageData,
                    success: function (res) {
                        if (res.data.url) {
                            var headurl = res.data.url;
                            lobsterlayui.GETAPI('tsTemplate.saveheadpic', { formid: formid, headpic: headurl });
                        }
                    }
                });
            });
        }, 3000);//延时3秒
    })
})